//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.apache.rocketmq.spring.annotation;

import java.lang.reflect.AnnotatedElement;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.apache.rocketmq.spring.support.RocketMQMessageListenerContainerRegistrar;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.OrderComparator;
import org.springframework.core.annotation.AnnotationUtils;

public class RocketMQMessageListenerBeanPostProcessor implements ApplicationContextAware, BeanPostProcessor, InitializingBean {
    private ApplicationContext applicationContext;
    private AnnotationEnhancer enhancer;
    private RocketMQMessageListenerContainerRegistrar listenerContainerRegistrar;

    public RocketMQMessageListenerBeanPostProcessor() {
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        RocketMQMessageListener ann = (RocketMQMessageListener)targetClass.getAnnotation(RocketMQMessageListener.class);
        if (ann != null) {
            RocketMQMessageListener enhance = this.enhance(targetClass, ann);
            if (this.listenerContainerRegistrar != null) {
                this.listenerContainerRegistrar.registerContainer(beanName, bean, enhance);
            }
        }

        return bean;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void afterPropertiesSet() throws Exception {
        this.buildEnhancer();
        this.listenerContainerRegistrar = (RocketMQMessageListenerContainerRegistrar)this.applicationContext.getBean(RocketMQMessageListenerContainerRegistrar.class);
    }

    private void buildEnhancer() {
        if (this.applicationContext != null) {
            Map<String, AnnotationEnhancer> enhancersMap = this.applicationContext.getBeansOfType(AnnotationEnhancer.class, false, false);
            if (enhancersMap.size() > 0) {
                List<AnnotationEnhancer> enhancers = (List)enhancersMap.values().stream().sorted(new OrderComparator()).collect(Collectors.toList());
                this.enhancer = (attrs, element) -> {
                    Map<String, Object> newAttrs = attrs;

                    AnnotationEnhancer enh;
                    for(Iterator var4 = enhancers.iterator(); var4.hasNext(); newAttrs = (Map)enh.apply(newAttrs, element)) {
                        enh = (AnnotationEnhancer)var4.next();
                    }

                    return attrs;
                };
            }
        }

    }

    private RocketMQMessageListener enhance(AnnotatedElement element, RocketMQMessageListener ann) {
        return this.enhancer == null ? ann : (RocketMQMessageListener)AnnotationUtils.synthesizeAnnotation((Map)this.enhancer.apply(AnnotationUtils.getAnnotationAttributes(ann), element), RocketMQMessageListener.class, (AnnotatedElement)null);
    }

    public interface AnnotationEnhancer extends BiFunction<Map<String, Object>, AnnotatedElement, Map<String, Object>> {
    }
}
